package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.MD5Utils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/8/7 0007.
 */

public class ForgetPasswordModel implements ForgetPasswordModelImpl {

    private static final String             TAG = "ForgetPasswordModel";
    private OnRequestListener               mListener;

    public ForgetPasswordModel(OnRequestListener listener) {
        mListener = listener;
    }

    /**
     * 返回验证码，修改密码
     * @param email 手机号
     * @param pass 密码
     * @param safeCode 验证码
     */
    @Override
    public void getEditPasswordResult(String email, String pass, String safeCode) {

        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("email", email)
                .addFormDataPart("pass",MD5Utils.getMd5Value(pass))
                .addFormDataPart("code",safeCode)
                .build();
        //不知道什么Bug反正这么写就不好使
//        Observable<CommonBean> loginResult = DataManager.getEditPasswordResult(
//                email, MD5Utils.getMd5Value(pass), safeCode);
        Observable<CommonBean> loginResult = DataManager.getEditPasswordResult(body);
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onEditSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    /**
     * 请求验证码
     * @param email 手机号
     */
    @Override
    public void getSafeCodeResult(String email) {
        Observable<CommonBean> loginResult = DataManager.getSafeCodeResult(
                email
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onGetSafeCodeSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void onEditSuccess(CommonBean bean);
        void onGetSafeCodeSuccess(CommonBean bean);
        void onError(Throwable e);
    }
}
